﻿using System;
using System.Collections.Generic;
using BattleCity.Classes;
using BattleCity.GUI;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace BattleCity
{
    public class BattleCityGame : Game
    {
        public readonly Point GameScreenSize = new Point(480, 416);
        public readonly Rectangle SpriteRec16 = new Rectangle(0, 0, 16, 16);
        public readonly Rectangle SpriteRec32 = new Rectangle(0, 0, 32, 32);
        public const int InfoPanelSize = 64;

        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        //Экран меню
        public MenuScreen menu;
        //Игровой экран
        public GameScreen newGame;
        //Экран Game Over
        GameOverScreen gameOver;
        //Экран смены уровня
        ChangeLevelScreen cls;
        //Экран с очками
        HiScoreScreen hi;
        FinishGameScreen finishScreen;

        //Номер уровня
        byte levelNumber;

        //Список текстур бонусов
        List<Texture2D> bonusList;
        //Список анимаций игрока
        Dictionary<string, Animation> animList;
        //Список звуков игрока
        Dictionary<string, SoundEffect> soundList;
        //Двумерный массив анимаций на 8 типов врагов
        Dictionary<string,Animation>[] enemyAnimList;
        //Список звуков снаряда
        public Dictionary<string, SoundEffect> ShootSoundList;
        //Список анимаций снаряда
        public Dictionary<string, Animation> ShootAnimList;
        //Список анимаций базы
        List<Animation> baseAnimList;
        //Список текстур карт
        List<Texture2D> mapTxt;
        //Список звуков игры
        List<SoundEffect> gameSounds;
        //Список текстур интерфейса
        List<Texture2D> ifTxt;
        List<Texture2D> hiList;

        Texture2D stageGui;
        SpriteFont font;
        SoundEffect pauseSound, gameOverSound, introSound;

        bool isPaused, isPauseKeyDown, isUp, isDown, isGameOver;
        double pauseTime, frame;

        bool _isStarted;
        public bool IsStarted
        {
            get { return _isStarted; }
            set { _isStarted = value; }
        }
        bool _isInGame;
        public bool IsInGame
        {
            get { return _isInGame; }
            set { _isInGame = value; }
        }

        public BattleCityGame()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            levelNumber = 1;
        }
        protected override void Initialize()
        {
            IsFixedTimeStep = false;
            graphics.PreferredBackBufferWidth = GameScreenSize.X;
            graphics.PreferredBackBufferHeight = GameScreenSize.Y;
            graphics.ApplyChanges();
            base.Initialize();
        }
        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            Services.AddService(typeof(SpriteBatch), spriteBatch);
            pauseSound = Content.Load<SoundEffect>("Sounds/pause");
            gameOverSound = Content.Load<SoundEffect>("Sounds/gameOver");
            #region Элементы экрана с очками
            hiList = new List<Texture2D>();
            hiList.Add(Content.Load<Texture2D>("gui/arrow"));
            hiList.Add(Content.Load<Texture2D>("Enemy/Type_1/enemy_Up"));
            hiList.Add(Content.Load<Texture2D>("Enemy/Type_2/enemy_Up"));
            hiList.Add(Content.Load<Texture2D>("Enemy/Type_3/enemy_Up"));
            hiList.Add(Content.Load<Texture2D>("Enemy/Type_4/enemy_Up"));
            hiList.Add(Content.Load<Texture2D>("gui/line"));
            hiList.Add(Content.Load<Texture2D>("gui/backScore")); 
            #endregion
            #region Графика интерфейса
            font = Content.Load<SpriteFont>("Press Start K");
            ifTxt = new List<Texture2D>();
            ifTxt.Add(Content.Load<Texture2D>("gui/back"));
            ifTxt.Add(Content.Load<Texture2D>("gui/tank"));
            ifTxt.Add(Content.Load<Texture2D>("gui/live"));
            ifTxt.Add(Content.Load<Texture2D>("gui/1p"));
            ifTxt.Add(Content.Load<Texture2D>("gui/level"));
            #endregion
            #region Анимации базы
            baseAnimList = new List<Animation>();
            baseAnimList.Add(new Animation(Content.Load<Texture2D>("base"), 1f, true));
            baseAnimList.Add(new Animation(Content.Load<Texture2D>("destroy_base"), 0.1f, false));
            #endregion
            #region Звуки экрана очков
            gameSounds = new List<SoundEffect>();
            gameSounds.Add(Content.Load<SoundEffect>("Sounds/hi"));
            gameSounds.Add(Content.Load<SoundEffect>("Sounds/total"));
            #endregion
            #region Звуки снаряда
            ShootSoundList = new Dictionary<string, SoundEffect>();
            ShootSoundList.Add("blockSound", Content.Load<SoundEffect>("Sounds/block"));
            ShootSoundList.Add("eraseSound", Content.Load<SoundEffect>("Sounds/brickErase"));
            ShootSoundList.Add("shootSound", Content.Load<SoundEffect>("Sounds/shoot"));
            #endregion
            #region Анимации снаряда
            ShootAnimList = new Dictionary<string, Animation>();
            ShootAnimList.Add("moveUp", new Animation(Content.Load<Texture2D>("Shoot/shoot_Up"), 1f, true));
            ShootAnimList.Add("moveDown", new Animation(Content.Load<Texture2D>("Shoot/shoot_Down"), 1f, true));
            ShootAnimList.Add("moveLeft", new Animation(Content.Load<Texture2D>("Shoot/shoot_Left"), 1f, true));
            ShootAnimList.Add("moveRight", new Animation(Content.Load<Texture2D>("Shoot/shoot_Right"), 1f, true));
            #endregion
            #region Анимации игрока
            animList = new Dictionary<string, Animation>();
            animList.Add("moveUp", new Animation(Content.Load<Texture2D>("Player/me_moveUp"), 0.005f, true));
            animList.Add("moveDown", new Animation(Content.Load<Texture2D>("Player/me_moveDown"), 0.005f, true));
            animList.Add("moveLeft", new Animation(Content.Load<Texture2D>("Player/me_moveLeft"), 0.005f, true));
            animList.Add("moveRight", new Animation(Content.Load<Texture2D>("Player/me_moveRight"), 0.005f, true));
            animList.Add("idleUp", new Animation(Content.Load<Texture2D>("Player/me_Up"), 1f, true));
            animList.Add("idleDown", new Animation(Content.Load<Texture2D>("Player/me_Down"), 1f, true));
            animList.Add("idleLeft", new Animation(Content.Load<Texture2D>("Player/me_Left"), 1f, true));
            animList.Add("idleRight", new Animation(Content.Load<Texture2D>("Player/me_Right"), 1f, true));
            animList.Add("portal", new Animation(Content.Load<Texture2D>("portal"), 0.1f, true));
            animList.Add("destroy", new Animation(Content.Load<Texture2D>("destroy"), 0.1f, false));
            animList.Add("godModeUp", new Animation(Content.Load<Texture2D>("Player/godMode/me_Up"), 0.08f, true));
            animList.Add("godModeDown", new Animation(Content.Load<Texture2D>("Player/godMode/me_Down"), 0.08f, true));
            animList.Add("godModeLeft", new Animation(Content.Load<Texture2D>("Player/godMode/me_Left"), 0.08f, true));
            animList.Add("godModeRight", new Animation(Content.Load<Texture2D>("Player/godMode/me_Right"), 0.08f, true));
            #endregion
            #region Анимации противника
            enemyAnimList = new Dictionary<string, Animation>[8];
            for (int i = 0; i < 8; i++)
                enemyAnimList[i] = new Dictionary<string, Animation>();
            Animation emptyTex = new Animation(Content.Load<Texture2D>("empty"), 0.1f, false);
            #region Противник типа 1
            enemyAnimList[0]["moveUp"] = new Animation(Content.Load<Texture2D>("Enemy/Type_1/enemy_moveUp"), 0.005f, true);
            enemyAnimList[0]["moveDown"] = new Animation(Content.Load<Texture2D>("Enemy/Type_1/enemy_moveDown"), 0.005f, true);
            enemyAnimList[0]["moveLeft"] = new Animation(Content.Load<Texture2D>("Enemy/Type_1/enemy_moveLeft"), 0.005f, true);
            enemyAnimList[0]["moveRight"] = new Animation(Content.Load<Texture2D>("Enemy/Type_1/enemy_moveRight"), 0.005f, true);
            enemyAnimList[0]["idleUp"] = new Animation(Content.Load<Texture2D>("Enemy/Type_1/enemy_Up"), 1f, true);
            enemyAnimList[0]["idleDown"] = new Animation(Content.Load<Texture2D>("Enemy/Type_1/enemy_Down"), 1f, true);
            enemyAnimList[0]["idleLeft"] = new Animation(Content.Load<Texture2D>("Enemy/Type_1/enemy_Left"), 1f, true);
            enemyAnimList[0]["idleRight"] = new Animation(Content.Load<Texture2D>("Enemy/Type_1/enemy_Right"), 1f, true);
            enemyAnimList[0]["portal"] = animList["portal"];
            enemyAnimList[0]["empty"] = emptyTex;
            enemyAnimList[0]["destroy"] = animList["destroy"];
            #endregion
            #region Противник типа 2
            enemyAnimList[1]["moveUp"] = new Animation(Content.Load<Texture2D>("Enemy/Type_2/enemy_moveUp"), 0.005f, true);
            enemyAnimList[1]["moveDown"] = new Animation(Content.Load<Texture2D>("Enemy/Type_2/enemy_moveDown"), 0.005f, true);
            enemyAnimList[1]["moveLeft"] = new Animation(Content.Load<Texture2D>("Enemy/Type_2/enemy_moveLeft"), 0.005f, true);
            enemyAnimList[1]["moveRight"] = new Animation(Content.Load<Texture2D>("Enemy/Type_2/enemy_moveRight"), 0.005f, true);
            enemyAnimList[1]["idleUp"] = new Animation(Content.Load<Texture2D>("Enemy/Type_2/enemy_Up"), 1f, true);
            enemyAnimList[1]["idleDown"] = new Animation(Content.Load<Texture2D>("Enemy/Type_2/enemy_Down"), 1f, true);
            enemyAnimList[1]["idleLeft"] = new Animation(Content.Load<Texture2D>("Enemy/Type_2/enemy_Left"), 1f, true);
            enemyAnimList[1]["idleRight"] = new Animation(Content.Load<Texture2D>("Enemy/Type_2/enemy_Right"), 1f, true);
            enemyAnimList[1]["portal"] = animList["portal"];
            enemyAnimList[1]["empty"] = emptyTex;
            enemyAnimList[1]["destroy"] = animList["destroy"];
            #endregion
            #region Противник типа 3
            enemyAnimList[2]["moveUp"] = new Animation(Content.Load<Texture2D>("Enemy/Type_3/enemy_moveUp"), 0.005f, true);
            enemyAnimList[2]["moveDown"] = new Animation(Content.Load<Texture2D>("Enemy/Type_3/enemy_moveDown"), 0.005f, true);
            enemyAnimList[2]["moveLeft"] = new Animation(Content.Load<Texture2D>("Enemy/Type_3/enemy_moveLeft"), 0.005f, true);
            enemyAnimList[2]["moveRight"] = new Animation(Content.Load<Texture2D>("Enemy/Type_3/enemy_moveRight"), 0.005f, true);
            enemyAnimList[2]["idleUp"] = new Animation(Content.Load<Texture2D>("Enemy/Type_3/enemy_Up"), 1f, true);
            enemyAnimList[2]["idleDown"] = new Animation(Content.Load<Texture2D>("Enemy/Type_3/enemy_Down"), 1f, true);
            enemyAnimList[2]["idleLeft"] = new Animation(Content.Load<Texture2D>("Enemy/Type_3/enemy_Left"), 1f, true);
            enemyAnimList[2]["idleRight"] = new Animation(Content.Load<Texture2D>("Enemy/Type_3/enemy_Right"), 1f, true);
            enemyAnimList[2]["portal"] = animList["portal"];
            enemyAnimList[2]["empty"] = emptyTex;
            enemyAnimList[2]["destroy"] = animList["destroy"];
            #endregion
            #region Противник типа 4
            enemyAnimList[3]["moveUp"] = new Animation(Content.Load<Texture2D>("Enemy/Type_4/enemy_moveUp"), 0.005f, true);
            enemyAnimList[3]["moveDown"] = new Animation(Content.Load<Texture2D>("Enemy/Type_4/enemy_moveDown"), 0.005f, true);
            enemyAnimList[3]["moveLeft"] = new Animation(Content.Load<Texture2D>("Enemy/Type_4/enemy_moveLeft"), 0.005f, true);
            enemyAnimList[3]["moveRight"] = new Animation(Content.Load<Texture2D>("Enemy/Type_4/enemy_moveRight"), 0.005f, true);
            enemyAnimList[3]["idleUp"] = new Animation(Content.Load<Texture2D>("Enemy/Type_4/enemy_Up"), 1f, true);
            enemyAnimList[3]["idleDown"] = new Animation(Content.Load<Texture2D>("Enemy/Type_4/enemy_Down"), 1f, true);
            enemyAnimList[3]["idleLeft"] = new Animation(Content.Load<Texture2D>("Enemy/Type_4/enemy_Left"), 1f, true);
            enemyAnimList[3]["idleRight"] = new Animation(Content.Load<Texture2D>("Enemy/Type_4/enemy_Right"), 1f, true);
            enemyAnimList[3]["portal"] = animList["portal"];
            enemyAnimList[3]["empty"] = emptyTex;
            enemyAnimList[3]["destroy"] = animList["destroy"];
            enemyAnimList[3]["moveUpG"] = new Animation(Content.Load<Texture2D>("Enemy/Type_4/enemy_moveUp_G"), 0.4f, true);
            enemyAnimList[3]["moveDownG"] = new Animation(Content.Load<Texture2D>("Enemy/Type_4/enemy_moveDown_G"), 0.4f, true);
            enemyAnimList[3]["moveLeftG"] = new Animation(Content.Load<Texture2D>("Enemy/Type_4/enemy_moveLeft_G"), 0.4f, true);
            enemyAnimList[3]["moveRightG"] = new Animation(Content.Load<Texture2D>("Enemy/Type_4/enemy_moveRight_G"), 0.4f, true);
            enemyAnimList[3]["idleUpG"] = new Animation(Content.Load<Texture2D>("Enemy/Type_4/enemy_Up_G"), 1f, true);
            enemyAnimList[3]["idleDownG"] = new Animation(Content.Load<Texture2D>("Enemy/Type_4/enemy_Down_G"), 1f, true);
            enemyAnimList[3]["idleLeftG"] = new Animation(Content.Load<Texture2D>("Enemy/Type_4/enemy_Left_G"), 1f, true);
            enemyAnimList[3]["idleRightG"] = new Animation(Content.Load<Texture2D>("Enemy/Type_4/enemy_Right_G"), 1f, true);
            enemyAnimList[3]["moveUpY"] = new Animation(Content.Load<Texture2D>("Enemy/Type_4/enemy_moveUp_Y"), 0.4f, true);
            enemyAnimList[3]["moveDownY"] = new Animation(Content.Load<Texture2D>("Enemy/Type_4/enemy_moveDown_Y"), 0.4f, true);
            enemyAnimList[3]["moveLeftY"] = new Animation(Content.Load<Texture2D>("Enemy/Type_4/enemy_moveLeft_Y"), 0.4f, true);
            enemyAnimList[3]["moveRightY"] = new Animation(Content.Load<Texture2D>("Enemy/Type_4/enemy_moveRight_Y"), 0.4f, true);
            enemyAnimList[3]["idleUpY"] = new Animation(Content.Load<Texture2D>("Enemy/Type_4/enemy_Up_Y"), 1f, true);
            enemyAnimList[3]["idleDownY"] = new Animation(Content.Load<Texture2D>("Enemy/Type_4/enemy_Down_Y"), 1f, true);
            enemyAnimList[3]["idleLeftY"] = new Animation(Content.Load<Texture2D>("Enemy/Type_4/enemy_Left_Y"), 1f, true);
            enemyAnimList[3]["idleRightY"] = new Animation(Content.Load<Texture2D>("Enemy/Type_4/enemy_Right_Y"), 1f, true);
            #endregion
            #region Противник типа 5
            enemyAnimList[4]["moveUp"] = new Animation(Content.Load<Texture2D>("Enemy/Type_5/enemy_moveUp"), 0.005f, true);
            enemyAnimList[4]["moveDown"] = new Animation(Content.Load<Texture2D>("Enemy/Type_5/enemy_moveDown"), 0.005f, true);
            enemyAnimList[4]["moveLeft"] = new Animation(Content.Load<Texture2D>("Enemy/Type_5/enemy_moveLeft"), 0.005f, true);
            enemyAnimList[4]["moveRight"] = new Animation(Content.Load<Texture2D>("Enemy/Type_5/enemy_moveRight"), 0.005f, true);
            enemyAnimList[4]["idleUp"] = new Animation(Content.Load<Texture2D>("Enemy/Type_5/enemy_Up"), 1f, true);
            enemyAnimList[4]["idleDown"] = new Animation(Content.Load<Texture2D>("Enemy/Type_5/enemy_Down"), 1f, true);
            enemyAnimList[4]["idleLeft"] = new Animation(Content.Load<Texture2D>("Enemy/Type_5/enemy_Left"), 1f, true);
            enemyAnimList[4]["idleRight"] = new Animation(Content.Load<Texture2D>("Enemy/Type_5/enemy_Right"), 1f, true);
            enemyAnimList[4]["portal"] = animList["portal"];
            enemyAnimList[4]["empty"] = emptyTex;
            enemyAnimList[4]["destroy"] = animList["destroy"];
            #endregion
            #region Противник типа 6
            enemyAnimList[5]["moveUp"] = new Animation(Content.Load<Texture2D>("Enemy/Type_6/enemy_moveUp"), 0.005f, true);
            enemyAnimList[5]["moveDown"] = new Animation(Content.Load<Texture2D>("Enemy/Type_6/enemy_moveDown"), 0.005f, true);
            enemyAnimList[5]["moveLeft"] = new Animation(Content.Load<Texture2D>("Enemy/Type_6/enemy_moveLeft"), 0.005f, true);
            enemyAnimList[5]["moveRight"] = new Animation(Content.Load<Texture2D>("Enemy/Type_6/enemy_moveRight"), 0.005f, true);
            enemyAnimList[5]["idleUp"] = new Animation(Content.Load<Texture2D>("Enemy/Type_6/enemy_Up"), 1f, true);
            enemyAnimList[5]["idleDown"] = new Animation(Content.Load<Texture2D>("Enemy/Type_6/enemy_Down"), 1f, true);
            enemyAnimList[5]["idleLeft"] = new Animation(Content.Load<Texture2D>("Enemy/Type_6/enemy_Left"), 1f, true);
            enemyAnimList[5]["idleRight"] = new Animation(Content.Load<Texture2D>("Enemy/Type_6/enemy_Right"), 1f, true);
            enemyAnimList[5]["portal"] = animList["portal"];
            enemyAnimList[5]["empty"] = emptyTex;
            enemyAnimList[5]["destroy"] = animList["destroy"];
            #endregion
            #region Противник типа 7
            enemyAnimList[6]["moveUp"] = new Animation(Content.Load<Texture2D>("Enemy/Type_7/enemy_moveUp"), 0.005f, true);
            enemyAnimList[6]["moveDown"] = new Animation(Content.Load<Texture2D>("Enemy/Type_7/enemy_moveDown"), 0.005f, true);
            enemyAnimList[6]["moveLeft"] = new Animation(Content.Load<Texture2D>("Enemy/Type_7/enemy_moveLeft"), 0.005f, true);
            enemyAnimList[6]["moveRight"] = new Animation(Content.Load<Texture2D>("Enemy/Type_7/enemy_moveRight"), 0.005f, true);
            enemyAnimList[6]["idleUp"] = new Animation(Content.Load<Texture2D>("Enemy/Type_7/enemy_Up"), 1f, true);
            enemyAnimList[6]["idleDown"] = new Animation(Content.Load<Texture2D>("Enemy/Type_7/enemy_Down"), 1f, true);
            enemyAnimList[6]["idleLeft"] = new Animation(Content.Load<Texture2D>("Enemy/Type_7/enemy_Left"), 1f, true);
            enemyAnimList[6]["idleRight"] = new Animation(Content.Load<Texture2D>("Enemy/Type_7/enemy_Right"), 1f, true);
            enemyAnimList[6]["portal"] = animList["portal"];
            enemyAnimList[6]["empty"] = emptyTex;
            enemyAnimList[6]["destroy"] = animList["destroy"];
            #endregion
            #region Противник типа 8
            enemyAnimList[7]["moveUp"] = new Animation(Content.Load<Texture2D>("Enemy/Type_8/enemy_moveUp"), 0.005f, true);
            enemyAnimList[7]["moveDown"] = new Animation(Content.Load<Texture2D>("Enemy/Type_8/enemy_moveDown"), 0.005f, true);
            enemyAnimList[7]["moveLeft"] = new Animation(Content.Load<Texture2D>("Enemy/Type_8/enemy_moveLeft"), 0.005f, true);
            enemyAnimList[7]["moveRight"] = new Animation(Content.Load<Texture2D>("Enemy/Type_8/enemy_moveRight"), 0.005f, true);
            enemyAnimList[7]["idleUp"] = new Animation(Content.Load<Texture2D>("Enemy/Type_8/enemy_Up"), 1f, true);
            enemyAnimList[7]["idleDown"] = new Animation(Content.Load<Texture2D>("Enemy/Type_8/enemy_Down"), 1f, true);
            enemyAnimList[7]["idleLeft"] = new Animation(Content.Load<Texture2D>("Enemy/Type_8/enemy_Left"), 1f, true);
            enemyAnimList[7]["idleRight"] = new Animation(Content.Load<Texture2D>("Enemy/Type_8/enemy_Right"), 1f, true);
            enemyAnimList[7]["portal"] = animList["portal"];
            enemyAnimList[7]["empty"] = emptyTex;
            enemyAnimList[7]["destroy"] = animList["destroy"];
            #endregion
            #endregion
            #region Загружаем текстуры карты
            mapTxt = new List<Texture2D>();
            mapTxt.Add(Content.Load<Texture2D>("wall"));
            mapTxt.Add(Content.Load<Texture2D>("stone"));
            mapTxt.Add(Content.Load<Texture2D>("water"));
            mapTxt.Add(Content.Load<Texture2D>("grass"));
            mapTxt.Add(Content.Load<Texture2D>("ice"));
            #endregion
            #region Звуки игрока
            soundList = new Dictionary<string, SoundEffect>();
            soundList.Add("moveSound", Content.Load<SoundEffect>("Sounds/move"));
            soundList.Add("dieSound", Content.Load<SoundEffect>("Sounds/buh"));
            soundList.Add("engineSound", Content.Load<SoundEffect>("Sounds/engine"));
            soundList.Add("bonusSound", Content.Load<SoundEffect>("Sounds/getBonus"));
            soundList.Add("lifeSound", Content.Load<SoundEffect>("Sounds/getLife"));
            soundList.Add("skillSound", Content.Load<SoundEffect>("Sounds/star"));
            #endregion
            #region Текстуры бонусов
            bonusList = new List<Texture2D>();
            for (int i = 1; i <= 6; i++)
                bonusList.Add(Content.Load<Texture2D>("Bonus/bonus_" + i.ToString()));
            #endregion
            #region Экран игры
            stageGui = Content.Load<Texture2D>("gui/stage");
            introSound = Content.Load<SoundEffect>("Sounds/intro");
            NewGameStart();
            #endregion 
            #region Экран Game Over
            gameOver = new GameOverScreen(this, Content.Load<Texture2D>("gui/gameover_screen"));
            gameOver.TimeExpired += new EventHandler(OnGameOverTimeExpired);
            Components.Add(gameOver);
            #endregion 
            #region Экран меню
            CreateMenuScreen();
            #endregion
            #region Экран успешного прохождения игры
            finishScreen = new FinishGameScreen(this, Content.Load<Texture2D>("gui/finish"));
            Components.Add(finishScreen);
            #endregion
            hi = new HiScoreScreen(this, font, hiList, gameSounds, levelNumber, newGame.HiScore);
            hi.TimeExpired += new EventHandler(OnHiScoreTimeExpired);
            Components.Add(hi);
            //Отображаем меню, остальные элементы скрыты
            menu.Show();
        }
        protected override void UnloadContent()
        {
            Content.Unload();
        }
        //Создание экрана уровня
        void NewGameStart()
        {
            cls = new ChangeLevelScreen(this, stageGui, font, levelNumber);
            cls.Hided += (s, e) =>
            {
                _isStarted = false;
                newGame.Show();
                IsInGame = true;
            };
            Components.Add(cls);
            newGame = new GameScreen(this, levelNumber, bonusList, animList, soundList, enemyAnimList, ShootSoundList, ShootAnimList, baseAnimList, mapTxt, introSound, ifTxt, font);
            newGame.LevelCompleted += new EventHandler(OnLevelCompleted);
            newGame.GameOver += new EventHandler(OnGameOver);
            Components.Add(newGame); 
            _isStarted = true;
            isGameOver = false;
        }
        //Создание экрана меню
        void CreateMenuScreen()
        {
            List<MenuItem> menuItems = new List<MenuItem>(2);
            Action firstAction = new Action(() =>
            {
                menu.Hide();
                if (levelNumber > 1)
                    NewGameStart();
                cls.Show();
                _isStarted = true;
            });
            Action secondAction = new Action(() => { this.Exit(); });
            menuItems.Add(new MenuItem(Content.Load<Texture2D>("gui/1player"), new Vector2(180f, 657f), Color.White, firstAction));
            menuItems.Add(new MenuItem(Content.Load<Texture2D>("gui/exit"), new Vector2(180f, 688f), Color.DarkGray, secondAction));

            menu = new MenuScreen(this, Content.Load<Texture2D>("Player/me_Right"), Content.Load<Texture2D>("gui/startScreen"), menuItems);
            Components.Add(menu); 
        }
        //Обработка нажатий на клавиши
        void KeyboardHandle()
        {
            //Считаем состояние клавиатуры
            KeyboardState kbState = Keyboard.GetState();
            //Если выбран экран меню
            if (menu.Enabled)
            {
                //Проверяем нажаты ли клавиши Вверх или Вниз
                bool Up = kbState.IsKeyDown(Keys.Up);
                bool Down = kbState.IsKeyDown(Keys.Down);
                if (Up && !isUp)
                    menu.CurrentItem--;
                if (Down && !isDown)
                    menu.CurrentItem++;
                isUp = Up;
                isDown = Down;
                //При нажатии клавиши Enter выполняем команду, соответствующую текущему пункту меню
                if (kbState.IsKeyDown(Keys.Enter))
                    menu.DoItemAction();
            }
            //Если активен игровой экран
            //При нажатии Esc закрываем его и открываем меню
            if (newGame.Enabled)
            {
                if (kbState.IsKeyDown(Keys.Escape))
                {
                    newGame.Hide();
                    menu.Show();
                    IsInGame = false;
                }
            }
            //Проверяем на паузу
            if (IsInGame)
            {
                bool isP = kbState.IsKeyDown(Keys.P);
                if (isP && !isPauseKeyDown)
                {
                    pauseSound.Play();
                    isPaused = !isPaused;
                }
                isPauseKeyDown = isP;
            }
        }
        void OnLevelCompleted(object sender, EventArgs e)
        {
            IsInGame = false;
            newGame.Hide();
            hi.Show();
        }
        void OnGameOver(object sender, EventArgs e)
        {
            IsInGame = false;
            isGameOver = true;
            newGame.Hide();
            hi.Show();
        }
        void OnHiScoreTimeExpired(object sender, EventArgs e)
        {
            hi.Hide();
            if (isGameOver)
            {
                gameOver.Show();
                gameOverSound.Play();
            }
            else
            {
                ++levelNumber;
                NewGameStart();
                newGame.Show();
                IsInGame = true;
            }
        }
        void OnGameOverTimeExpired(object sender, EventArgs e)
        {
            gameOver.Hide();
            menu.Show();
        }
        void DrawPauseMessage(GameTime time)
        {
            pauseTime += time.ElapsedGameTime.TotalSeconds;
            while (pauseTime > 0.3f)
            {
                pauseTime = 0;
                frame = (frame + 1) % 2;
            }
            if (frame == 1)
                spriteBatch.DrawString(font, "PAUSE", new Vector2(170f, 210f), Color.Red, 0f, new Vector2(0, 0), 1f, SpriteEffects.None, 0.55f);
            else if (frame == 2)
                spriteBatch.DrawString(font, "", new Vector2(170f, 210f), Color.Red, 0f, new Vector2(0, 0), 1f, SpriteEffects.None, 0.55f);
        }
        protected override void Update(GameTime gameTime)
        {
            KeyboardHandle();
            if (!isPaused)
                base.Update(gameTime);
        }
        protected override void Draw(GameTime gameTime)
        {
            graphics.GraphicsDevice.Clear(Color.Black);
            spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
            if (isPaused)
                DrawPauseMessage(gameTime);
            base.Draw(gameTime);
            spriteBatch.End();
        }
        protected override void Dispose(bool disposing)
        {
            menu.Dispose();
            newGame.Dispose();
            gameOver.Dispose();
            cls.Dispose();
            hi.Dispose();
            finishScreen.Dispose();
            base.Dispose(disposing);
        }
    }
}